package com.eascs.crm.common.config;

import com.eascs.app.context.ConfigContext;
import com.eascs.app.dao.ConnectionPoolDataSource;
import com.eascs.app.dao.DataSourceSetting;
import com.eascs.app.dao.JdbcSession;
import com.eascs.web.DefaultAppConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class AppConfig extends DefaultAppConfig {
	private static List initSqls = new ArrayList<String>();

	{
		initSqls.add("set names utf8mb4;");
	}

	@Bean
	public DataSourceSetting dataSourceSettingDb1() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db1.");
		return dataSourceSetting;
	}
	
	@Bean
	public DataSourceSetting oaDataSourceSetting() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db2.");
		return dataSourceSetting;
	}
	
	@Bean
	public DataSourceSetting DataSourceSettingForO2odev() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db3.");
		return dataSourceSetting;
	}


	@Bean
	public DataSource dataSourceDb1() {
		DataSourceSetting dataSourceSetting = dataSourceSettingDb1();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		connectionPoolDataSource.setConnectionInitSqls(initSqls);

		return connectionPoolDataSource;
	}
	

	@Bean
	public DataSource oaDataSource() {
		DataSourceSetting dataSourceSetting = oaDataSourceSetting();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}
	
	@Bean
	public DataSource o2odevDataSource() {
		DataSourceSetting dataSourceSetting = DataSourceSettingForO2odev();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		connectionPoolDataSource.setConnectionInitSqls(initSqls);
		return connectionPoolDataSource;
	}
	

	@Bean(name = "transactionManager")
	public PlatformTransactionManager transactionManagerDb1() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceDb1());
		return transactionManager;
	}
	
	@Bean(name = "oaTransactionManager")
	public PlatformTransactionManager oaTransactionManager() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(oaDataSource());
		return transactionManager;
	}

	@Bean(name = "o2odevtransactionManager")
	public PlatformTransactionManager o2odevTransactionManagerDb1() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(o2odevDataSource());
		return transactionManager;
	}

	@Bean(name = "jdbcSession")
	public JdbcSession jdbcSessionDb1() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceDb1());
		return jdbcSession;
	}
	
	@Bean(name = "oaJdbcSession")
	public JdbcSession oaJdbcSession() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.oaDataSource());
		return jdbcSession;
	}
	
	@Bean(name = "o2odevJdbcSession")
	public JdbcSession o2odevJdbcSession() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.o2odevDataSource());
		return jdbcSession;
	}
	
	@Bean
	public DataSourceSetting dataSourceSettingOracle() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db2.");
		return dataSourceSetting;
	}

	@Bean
	public DataSource dataSourceOracle() {
		DataSourceSetting dataSourceSetting = dataSourceSettingOracle();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}

	@Bean(name = "jdbcSessionOracle")
	public JdbcSession jdbcSessionOracle() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceOracle());
		return jdbcSession;
	}

	/*
	 * @Bean public DubboConsumerInterceptor dubboConsumerInterceptor() { return
	 * new DubboConsumerInterceptor(); }
	 */
	@Bean
	public static PropertySourcesPlaceholderConfigurer placehodlerConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}
}
